package com.dirk.happie.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.dirk.happie.R;
import com.dirk.happie.beans.SettingsBean;

public class Availablity {

	public static boolean isNetworkAvailable(Context context) {
		/*
		 * ConnectivityManager connectivityManager = (ConnectivityManager)
		 * context.getSystemService(Context.CONNECTIVITY_SERVICE); if
		 * (connectivityManager
		 * .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() ==
		 * NetworkInfo.State.CONNECTED ||
		 * connectivityManager.getNetworkInfo(ConnectivityManager
		 * .TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) { // we are
		 * connected to a network return true; } else { return false; }
		 */
		final ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isDeliveryAvailable(SettingsBean settings, Calendar c) {
		boolean retValue = false;
		int dayNow = c.get(Calendar.DAY_OF_WEEK);
		int hourNow = c.get(Calendar.HOUR_OF_DAY);
		int minuteNow = c.get(Calendar.MINUTE);

		switch (dayNow) {
		case Calendar.MONDAY:

			retValue = compare(settings.getMonOpen(), hourNow, minuteNow, settings.getDsatFrom(), settings.getDsatTo());
			break;

		case Calendar.TUESDAY:
			retValue = compare(settings.getTueOpen(), hourNow, minuteNow, settings.getDtueFrom(), settings.getDtueTo());
			break;

		case Calendar.WEDNESDAY:
			retValue = compare(settings.getWedOpen(), hourNow, minuteNow, settings.getDwedFrom(), settings.getDwedTo());
			break;

		case Calendar.THURSDAY:
			retValue = compare(settings.getThuOpen(), hourNow, minuteNow, settings.getDthuFrom(), settings.getDthuTo());
			break;

		case Calendar.FRIDAY:
			retValue = compare(settings.getFriOpen(), hourNow, minuteNow, settings.getDfriFrom(), settings.getDfriTo());
			break;

		case Calendar.SATURDAY:
			retValue = compare(settings.getSatOpen(), hourNow, minuteNow, settings.getDsatFrom(), settings.getDsatTo());
			break;

		case Calendar.SUNDAY:
			retValue = compare(settings.getSunOpen(), hourNow, minuteNow, settings.getDsunFrom(), settings.getDsunTo());
			break;

		default:
			retValue = false;
			break;
		}

		return retValue;

	}

	public static boolean isPickupAvailable(SettingsBean settings, Calendar c) {
		boolean retValue = false;
		int dayNow = c.get(Calendar.DAY_OF_WEEK);
		int hourNow = c.get(Calendar.HOUR_OF_DAY);
		int minuteNow = c.get(Calendar.MINUTE);
		switch (dayNow) {
		case Calendar.MONDAY:

			retValue = compare(settings.getMonOpen(), hourNow, minuteNow, settings.getPsatFrom(), settings.getPsatTo());
			break;

		case Calendar.TUESDAY:
			retValue = compare(settings.getTueOpen(), hourNow, minuteNow, settings.getPtueFrom(), settings.getPtueTo());
			break;

		case Calendar.WEDNESDAY:
			retValue = compare(settings.getWedOpen(), hourNow, minuteNow, settings.getPwedFrom(), settings.getPwedTo());
			break;

		case Calendar.THURSDAY:
			retValue = compare(settings.getThuOpen(), hourNow, minuteNow, settings.getPthuFrom(), settings.getPthuTo());
			break;

		case Calendar.FRIDAY:
			retValue = compare(settings.getFriOpen(), hourNow, minuteNow, settings.getPfriFrom(), settings.getPfriTo());
			break;

		case Calendar.SATURDAY:
			retValue = compare(settings.getSatOpen(), hourNow, minuteNow, settings.getPsatFrom(), settings.getPsatTo());
			break;

		case Calendar.SUNDAY:
			retValue = compare(settings.getSunOpen(), hourNow, minuteNow, settings.getPsunFrom(), settings.getPsunTo());
			break;
		}

		return retValue;

	}
	
	public static boolean isOpenDayOfWeek(SettingsBean settings, Calendar c) {
		boolean retValue = false;
		int dayNow = c.get(Calendar.DAY_OF_WEEK);

		switch (dayNow) {
		case Calendar.MONDAY:
			if (settings.getMonOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.TUESDAY:
			if (settings.getTueOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.WEDNESDAY:
			if (settings.getWedOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.THURSDAY:
			if (settings.getThuOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.FRIDAY:
			if (settings.getFriOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.SATURDAY:
			if (settings.getSatOpen().equals("Y")) {
				retValue = true;
			}
			break;

		case Calendar.SUNDAY:
			if (settings.getSatOpen().equals("Y")) {
				retValue = true;
			}
			break;

		default:
			retValue = false;
			break;
		}

		return retValue;
	}

	private static int getHour(String s) {

		return Integer.parseInt(s.substring(0, 2));

	}

	private static int getMinute(String s) {

		return Integer.parseInt(s.substring(3));

	}

	private static boolean compare(String isOpen, int hourNow, int minuteNow, String start, String end) {

		if (start.length() < 4) {
			start = "00:00";
		}
		if (end.length() < 4) {
			end = "00:00";
		}

		if (isOpen.equals("Y")) {
			int startHour = getHour(start);
			int startMinute = getMinute(start);
			int endHour = getHour(end);
			int endMinute = getMinute(end);
			if ((hourNow == startHour && minuteNow > startMinute) || (hourNow == endHour && minuteNow < endMinute) || (hourNow > startHour && hourNow < endHour)) {
				return true;
			} else
				return false;
		} else
			return false;

	}

	public static boolean isOpen(SettingsBean settings, Calendar c) {
		DateFormat outputDF = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
		Date nowDate = null;
		Date closedDay1 = null;
		Date closedDay2 = null;
		Date closedDay3 = null;
		Date closedDay4 = null;
		try {
			nowDate = outputDF.parse(Common.pad(c.get(Calendar.YEAR)) + "-" + Common.pad(c.get(Calendar.MONTH) + 1) + "-" + Common.pad(c.get(Calendar.DAY_OF_MONTH)));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (settings.getClosedDay1() != null && !settings.getClosedDay1().equals("")) {
			try {
				closedDay1 = outputDF.parse(settings.getClosedDay1());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (settings.getClosedDay2() != null && !settings.getClosedDay2().equals("")) {
			try {
				closedDay2 = outputDF.parse(settings.getClosedDay2());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (settings.getClosedDay3() != null && !settings.getClosedDay3().equals("")) {
			try {
				closedDay3 = outputDF.parse(settings.getClosedDay3());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (settings.getClosedDay4() != null && !settings.getClosedDay4().equals("")) {
			try {
				closedDay4 = outputDF.parse(settings.getClosedDay4());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (nowDate != null && !nowDate.equals(closedDay1) && !nowDate.equals(closedDay2) && !nowDate.equals(closedDay3) && !nowDate.equals(closedDay4)) {
			return true;
		} else {
			return false;
		}
	}

	public static Calendar getAvailableTime(Calendar c, Context context) {
		Calendar now = Calendar.getInstance();

		now.add(Calendar.MINUTE, 15);

		if (now.get(Calendar.YEAR) > c.get(Calendar.YEAR) || now.get(Calendar.MONTH) > c.get(Calendar.MONTH) || now.get(Calendar.DAY_OF_MONTH) > c.get(Calendar.DAY_OF_MONTH) || now.get(Calendar.HOUR_OF_DAY) > c.get(Calendar.HOUR_OF_DAY) || now.get(Calendar.MINUTE) > c.get(Calendar.MINUTE)) {
			Toast.makeText(context, context.getString(R.string.time_not_available), Toast.LENGTH_SHORT).show();
		}

		while (c.get(Calendar.YEAR) < now.get(Calendar.YEAR)) {
			c.add(Calendar.YEAR, 1);
		}

		while (c.get(Calendar.MONTH) < now.get(Calendar.MONTH)) {
			c.add(Calendar.MONTH, 1);
		}

		while (c.get(Calendar.DAY_OF_MONTH) < now.get(Calendar.DAY_OF_MONTH)) {
			c.add(Calendar.DAY_OF_MONTH, 1);
		}

		while (c.get(Calendar.HOUR_OF_DAY) < now.get(Calendar.HOUR_OF_DAY)) {
			c.add(Calendar.HOUR_OF_DAY, 1);
		}

		while (c.get(Calendar.MINUTE) < now.get(Calendar.MINUTE)) {
			c.add(Calendar.MINUTE, 1);
		}

		return c;
	}
}
